package com.ruyuan.eshop.data.migrate.tool.exception;

import com.ruyuan.eshop.data.migrate.tool.core.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 默认的Controller全局异常处理增强组件
 *
 * @author zhonghuashishan
 * @version 1.0
 **/
@Slf4j
@RestControllerAdvice
@Order
public class GlobalExceptionHandler {

    // =========== 系统级别未知异常 =========

    @ExceptionHandler(value = Exception.class)
    public JsonResult<Object> handle(Exception e) {
        log.error("[ 系统未知错误 ]", e);
        return JsonResult.buildError(CommonErrorCodeEnum.SYSTEM_UNKNOWN_ERROR);
    }

    /**
     * 系统自定义业务异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    public JsonResult<Object> handle(BizException e) {
        log.error("[ 业务异常 ]", e);
        return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
    }


}